var gulp = require('gulp'),
    os = require('os'),//代理中间件 the middleware of proxy    
    connect = require('gulp-connect');

const { createProxyMiddleware } = require('http-proxy-middleware');
var host = {
    path: './',
    port: 4000,
    html: 'index.html'
};
//mac chrome: "Google chrome", 
var browser = os.platform() === 'linux' ? 'Google chrome' : (os.platform() === 'darwin' ? 'Google chrome' : (os.platform() === 'win32' ? 'chrome' : 'firefox'));
var pkg = require('./package.json');
gulp.task('connect', function () {
    connect.server({
        host: '0.0.0.0',
        root: host.path,
        port: host.port,
        livereload: true,
        middleware: function (connect, opt) {
            //本地服务器端口号：localhost:4000
            //把route虚拟路径也加上 api
            //实际的接口url
            //例如:http://localhost:4000/api/localhost:3000/home
            // opt.route = '/api';
            // opt.changeOrigin = true;
            // opt.ws = true;          
            // var proxy = new Proxy(opt);
            // return [proxy];

            //http-proxy-middleware

            return [
                createProxyMiddleware('/api', {
                    target: 'http://localhost:3000',
                    changeOrigin: true,
                    pathRewrite: {
                        //路径重写规则 
                        '^/api': ''
                    }
                })
            ];

        }
    });
});


gulp.task('default', gulp.series('connect', () => {
    // Do something after a,b, and c are finished.
}));
